<?php
namespace app\wsns\controller;
use Predis\Client;

$client = new Client([
    'scheme' => 'tcp',
    'host'   => '127.0.0.1',
    'port'   => 6379,
]);

function getShardedListLengthWithLua($client, $prefix, $numShards) {
    $luaScript = <<<'EOL'
local total = 0
for i = 0, ARGV[1] do
    local key = KEYS[1] .. ":" .. i
    local len = redis.call('LLen', key)
    total = total + len
end
return total
EOL;

    $keys = [$prefix]; // KEYS[1]
    $args = [$numShards]; // ARGV[1]

    return $client->eval($luaScript, $keys, $args);
}

// 示例调用
$prefix = 'shard_list';
$numShards = 3;
echo "分片列表长度（Lua）：" . getShardedListLengthWithLua($client, $prefix, $numShards) . "\n";